<?php

namespace app\home\service;

use app\common\helpers\PayHelper;
use app\home\model\Order;
use app\home\model\UserOrder;
use think\Config;

class Pay44755 implements IPay
{
    private $config = null;
    private $helper = null;

    public function __construct($config)
    {
        $this->config = $config;
        $this->helper = PayHelper::getInstance($this->config['channel']);
    }

    public function createOrder($params)
    {
        return
        // 计费点映射关系
        $map_class = Config::get('MapPayClass');
        if(!isset($map_class[$params['channel']])){
            $this->helper->record('channel map not found');
            return false;
        }

        if(empty($params['uid']) || empty($params['session_id'])){
            $this->helper->record('session_id uid not found');
            return false;
        }

        if(strlen($params['session_id']) != 26){
            $this->helper->record('session_id len fail');
            return false;
        }

        // 登录验证
        if($this->verifyLogin($params) == false){
            $this->helper->record('登录验证失败');
            // puterror("登录验证失败");
        }

        $orderModel = new UserOrder();
        $data = $orderModel->create_order_old($params['userid'], $params['channel'], $params['stroeid'], 5,null);

        $info = json_decode($data, true);
        $return['orderid'] = $info['orderid'];
        $return['product_id'] = $params['stroeid'];
        $return['product_price'] = $info['price'];
        $return['product_name'] = $info['subject'];

        $data_Arr['code'] = 'success';
        $data_Arr['data'] = $return;

        return $data_Arr;
    }
    public function processNotify($params)
    {
        $split_str = str_repeat('-',80);
        $this->helper->record($split_str);
        $this->helper->record($params);

        if(empty($params['orderid']) || empty($params['cash']) || empty($params['sign'])){
            $this->helper->record('some params empty');
            return 0;
        }

        // 验证签名
        if($this->verifySign($params) == false){
            $this->helper->record('verify fail');
            return 0;
        }
        $this->helper->record('verify ok');

        // 特殊处理 对已经成功处理过的订单 直接返回成功
        $order = Order::get($params['extra']);
        if(!empty($order) && $order['status'] == 1){
            $this->helper->record($params['extra'].'订单重复');
            return 1;
        }

        // 发货
        $time = date('Y-m-d H:i:s', time());
        $re = Db_connect_server_pay_domian($params['extra'], $params['money'], $time);
        if ($re == 'success') {
            return 1;
        }

        $this->helper->record('delivery fail');
        return 0;
    }

    private function verifySign($data)
    {
        unset($data['channel']);
        $data['way_name'] = urldecode($data['way_name']);
        ksort($data);
        $buff = "";
        foreach ($data as $k => $v)
        {
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= $k . "=" . $v;
            }
        }

        return md5($buff.$this->config['app_key']) == $data['sign'];
    }

    private function verifyLogin($data)
    {
        $url = "http://www.44755.com/notify-login";

        $post = [
            'uid'=>$data['uid'],
            'session_id'=>$data['session_id'],
            'pid'=>1,
        ];

        $res = request_post($url,$post);
        if($res === false){
            return false;
        }

        if($res == 1){
            return true;
        }

        return false;
    }
}
